layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
    $ = layui.jquery;

    //登录按钮
    form.on("submit(login)",function(data){
        let _this  = this;
        $(this).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");
        $.ajax({
            url: "/login",
            type: "POST",
            data: data.field,
            success: function (data) {
                if (data.code == 200) {
                    setTimeout(function(){
                        var user=JSON.parse(data.data.user)
                        if(user.userType==0) {
                            $.ajax({
                                url: '/admin/role/' + user.roleId,
                                type: 'GET',
                                async: false,
                                success: function (da) {
                                    //角色是否可用
                                    if (da.useable == "0") {
                                        window.location.href = "/admin/";
                                    } else {
                                        layer.msg('您的角色权限不可用');
                                        $(_this).text('登陆').removeAttr('disabled').removeClass('layui-disabled');
                                    }
                                }

                            });
                        }else
                        {
                            layer.msg('您的账号还未通过申请！');
                            $(_this).text('登陆').removeAttr('disabled').removeClass('layui-disabled');
                        }

                    },1000);
                } else {
                    layer.msg('用户名或密码错误！');
                    $(_this).text('登陆').removeAttr('disabled').removeClass('layui-disabled');
                }
            }
        });
        return false;
    });








    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    });
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    });
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
});
